package com.example.library_demo.common;


import com.example.library_demo.enums.GlobalResponseResultEnum;
import com.example.library_demo.enums.IResultEnum;
import com.example.library_demo.util.DateUtilEx;

import java.io.Serializable;
import java.util.Date;


/**
 * @description: 返回结果实体类
 * @author: Eric Lee
 * @time: 2021/5/24 17:48
 */
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private boolean success = false;
    private Integer code;
    private String message;
    private String date;
    private T data;


    public static <T> Result<T> success() {
        return success(null);
    }

    /**
     * 根据boolean值返回
     *
     * @param flag
     * @param <T>
     * @return
     */
    public static <T> Result<T> status(boolean flag) {
        return flag ? success(GlobalResponseResultEnum.HANDEL_SUCCESS.getCode(), GlobalResponseResultEnum.HANDEL_SUCCESS.getMessage())
                : error(GlobalResponseResultEnum.HANDEL_FAIL.getCode(), GlobalResponseResultEnum.HANDEL_FAIL.getMessage());
    }

    public static <T> Result<T> success(Integer code, String message) {
        Result<T> result = new Result(true);
        result.setDate(DateUtilEx.dateFormat(new Date()));
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    /**
     * 返回成功，带输出结果
     *
     * @param obj 输出结果
     * @return Result<T>
     */
    public static <T> Result<T> success(T obj) {
        Result<T> result = new Result(true);
        result.setDate(DateUtilEx.dateFormat(new Date()));
        result.setData(obj);
        result.setCode(GlobalResponseResultEnum.SUCCESS.getCode());
        result.setMessage(GlobalResponseResultEnum.SUCCESS.getMessage());
        return result;
    }

    /**
     * 默认无参返回
     *
     * @param <T>
     * @return
     */
    public static <T> Result<T> error() {
        return error(GlobalResponseResultEnum.DEFAULT_ERROR);
    }

    /**
     * 返回失败，带错误代码和错误消息
     *
     * @param code    错误消息
     * @param message 错误代码
     * @return Result<T>
     */
    public static <T> Result<T> error(Integer code, String message) {
        Result<T> result = new Result(false);
        result.setDate(DateUtilEx.dateFormat(new Date()));
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    public static <T> Result<T> error(Integer code, String message, T obj) {
        Result<T> result = error(code, message);
        result.setData(obj);
        return result;
    }


    /**
     * 功能描述: <返回失败，带错误代码和错误消息>
     */
    public static <T> Result<T> error(IResultEnum resultEnum) {
        Result<T> result = new Result(false);
        result.setDate(DateUtilEx.dateFormat(new Date()));
        result.setCode(resultEnum.getCode());
        result.setMessage(resultEnum.getMessage());
        return result;
    }

    public Result() {
    }

    public Result(boolean success) {
        this.success = success;
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }


    @Override
    public String toString() {
        return "Result{" +
                "success=" + success +
                ", code=" + code +
                ", message='" + message + '\'' +
                ", date='" + date + '\'' +
                ", data=" + data +
                '}';
    }
}
